home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3d_lib.zip / VM.C < prev    next >
C/C++ Source or Header  |  1990-12-09  |  3KB  |  74 lines

  1. /* Vector multiplication
  2.  
  3.    Copyright (c) 1988 by Gus O'Donnell
  4.  
  5.    Revision history:
  6.  
  7.    Version 1.00         February 29, 1988       As released.
  8.  
  9.    Version 1.01         March 20, 1988          Created libraries for all
  10.                                                 memory models
  11.  
  12. */
  13. #include "3d.h"
  14. #include <float.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17.  
  18. typedef double HOMO [CMAX];
  19.  
  20. void    vec_mul (VERTEX *this_vec, MATRIX this_mat, VERTEX *prod)
  21.  
  22. /* Multiply a vertex in homogeneous coordinates by a 4x4 matrix.
  23.  
  24. Points in 3-space are converted to homogeneous coordinates prior to
  25. transformation.  This consists simply of adding a fourth coordinate
  26. w = 1.0, called the scale factor:
  27.  
  28.                     [x y z] -> [wx wy wz w]
  29.  
  30. Mathematically, the value of w is not important; 1.0 is chosen for
  31. convenience.  To convert back to world coordinates, divide each coordinate
  32. by w and drop the last coordinate.
  33.  
  34. The value of w does not change as a result of scaling, translation, or
  35. rotation, but it does change as a result of perspective transformation.
  36.  
  37. Given a vector v in homogeneous coordinates and a matrix m, each coordinate
  38. of the product p[j] is given by the dot product of v and column j of m,
  39. that is:
  40.  
  41.                      p [j] = v [0] * m [0] [j]
  42.                            + v [1] * m [1] [j]
  43.                            + v [2] * m [2] [j]
  44.                            + v [3] * m [3] [j]
  45.  
  46. */
  47.  
  48. {
  49.     HOMO tvec,pvec;                /* tvec is temporary storage for the
  50.                                       vertex to be multiplied, pvec is the
  51.                                       product. */
  52.     int cc,cl;
  53.  
  54.     for (cc = 0; cc < DIM; cc++)                    /* Copy this_vec to
  55.                                                        temp storage */
  56.     tvec [cc] = this_vec -> coord [cc];
  57.     tvec [CMAX - 1] = 1.0;                          /* Convert to homo-
  58.                                                        geneous coordinates */
  59.  
  60.     for (cl = 0; cl < CMAX; cl++)                   /* Outside loop to
  61.                                                        calculate each coor-
  62.                                                        dinate */
  63.     {
  64.         pvec [cl] = 0.0;
  65.         for (cc = 0; cc < CMAX; cc++)               /* Inside loop to cal-
  66.                                                        culate dot product */
  67.         pvec [cl] = pvec [cl]
  68.                        + tvec [cc] * this_mat [cc] [cl];
  69.     }
  70.     for (cc = 0; cc < DIM; cc++)                    /* Convert back to world
  71.                                                        coordinates */
  72.         prod -> coord [cc] = pvec [cc]/pvec [CMAX - 1];
  73. }
  74.